//
//  GHStudyViewController.m
//  GodHorses
//
//  Created by Mac on 2017/11/14.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "GHStudyViewController.h"
#import "GHStudyModel.h"
#import "GHStudyTableViewCell.h"
#import "STWebViewController.h"
#import "GHVideoModel.h"
@interface GHStudyViewController ()
@property(nonatomic, strong) NSMutableArray <GHStudyModel*>                    *dataSouce;
@property(nonatomic, strong) ZFPlayerView           *playeView;
@property(nonatomic, strong) UIView                     *videoView;
@end

@implementation GHStudyViewController


#pragma mark --vc 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"健康讲堂";
    self.dataSouce = [NSMutableArray new];
    self.tableView.frame = CGRectMake(0, 0, UIScreenWidth, UIScreenHeight  - 64);
    [self configSubView];
    
   
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
   
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.playeView pause];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 300)];
    header.backgroundColor = [UIColor whiteColor];
    UIImageView * bannerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 200)];
    bannerImageView.image = [UIImage imageNamed:@"qy_banner"];
    [header addSubview:bannerImageView];
    
    //视频
    UIView * videoView = [[UIView alloc] initWithFrame:CGRectMake(0, bannerImageView.st_bottom + 10, UIScreenWidth, 200)];
    self.videoView = videoView;
    [header addSubview:videoView];
    ZFPlayerView * playeView = [ZFPlayerView new];
    self.playeView = playeView;
    ZFPlayerModel * model = [ZFPlayerModel new];
    model.placeholderImage = [UIImage imageNamed:@"video_preview"];
    model.videoURL = [NSURL URLWithString:@"https://video-cdn.suiyueyule.com/20171009122453-lsho_0whV8MTl1AfY1Xpv1DqoVz7.ts.mp4?auth_key=1510722944-0-0-c6224cd99e899028ff849e17ebbd62c4"];
    model.fatherView = videoView;
    [playeView playerModel:model];
    
    
    NSString * title = @"公司简介:\n四川神马泉高溶氧饮品有限公司是一家专业从事高品质瓶装饮用水生产和销售的企业，公司总部坐落于中国唯一科技城绵阳市，生产基地距绵阳市40公里开外的龙门山脉脚下，并素有“西南第一泉” 之称的沸泉水源地-安州沸泉镇";
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(10, videoView.bottom + 1, UIScreenWidth - 20, 300)
                                                     text:title
                                                textColor:GH_secendTextColor
                                                     font:13
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    titleLable.backgroundColor = [UIColor whiteColor];
    [header addSubview:titleLable];
    titleLable.height = [title st_heigthWithwidth:UIScreenWidth - 20 font:13] + 30;
    header.height = titleLable.st_bottom + 10;
    titleLable.attributedText =   [titleLable.text st_convertAttributeStringWithKeyWord:@"公司简介:"
                                               attributes:@{NSForegroundColorAttributeName:GH_firstTextColor,
                                                            NSFontAttributeName:[UIFont systemFontOfSize:18]
                                                            }];
    
    self.tableView.tableHeaderView = header;
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendWebRequest];
         [self fetch_videorequset];
    }];
    [self.tableView.mj_header beginRefreshing];
    
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count ;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [GHStudyTableViewCell cellHeight ];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    GHStudyTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[GHStudyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.model = self.dataSouce[indexPath.row];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    GHStudyModel * model = self.dataSouce[indexPath.row];
    STWebViewController * webVC = [[STWebViewController alloc] initWithH5String:model.content];
    webVC.progressBackgroundColor = GH_backgroundColor;
    webVC.progressTintColor = GH_YellowBackGroundColor;
    webVC.forceTitle = model.title;
    [self.navigationController pushViewController:webVC animated:YES];
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark --NetWork Method
- (void)sendWebRequest{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/index/health"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 self.dataSouce = [NSMutableArray new];
                                                 NSDictionary * array = responseObject[@"data"];
                                                 for (NSDictionary * studyDic in array ) {
                                                     GHStudyModel * model = [GHStudyModel mj_objectWithKeyValues:studyDic];
                                                     [self.dataSouce addObject:model];
                                                 }
                                                 [self.tableView reloadData];
                                                 [self.tableView.mj_header endRefreshing];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                             }];
}
- (void)fetch_videorequset{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/index/get_video"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 NSDictionary * videoDic = [responseObject[@"data"] firstObject];
                                                 GHVideoModel *model = [GHVideoModel mj_objectWithKeyValues:videoDic];
                                                 ZFPlayerModel * zfmodel = [ZFPlayerModel new];
                                                 zfmodel.placeholderImageURLString = model.vedio_img;
                                                 zfmodel.videoURL = [NSURL URLWithString:model.vedio_url];
                                                 zfmodel.fatherView = _videoView;
                                                 [_playeView playerModel:zfmodel];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {

                                             }];
}
@end
